package com.example.demo;

import com.alibaba.fastjson.JSON;
import com.clearofchina.core.util.StringUtil;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.stream.Collector;
import java.util.stream.Collectors;

/**
 * @author gxing
 * @desc TODO
 * @date 2023/8/17
 */
public class MainTest4 {

    public static void main(String[] args) {
        SysRoleDataPermission l1 = new SysRoleDataPermission("53034");
        SysRoleDataPermission l2 = new SysRoleDataPermission("53035");
        SysRoleDataPermission l3 = new SysRoleDataPermission("53036", "60001");
        SysRoleDataPermission l4 = new SysRoleDataPermission("53036", "60002");
        SysRoleDataPermission l5 = new SysRoleDataPermission("53037", "70001", "10001");
        SysRoleDataPermission l6 = new SysRoleDataPermission("53037", "70001", "10002");
        SysRoleDataPermission l7 = new SysRoleDataPermission("53037", "70002", "20002");
        SysRoleDataPermission l8 = new SysRoleDataPermission("53037", "70002", "20003");

        List<SysRoleDataPermission> list = Arrays.asList(l1, l2, l3, l4, l5, l6, l7, l8);
        Map<String, List<SysRoleDataPermission>> orgMap = list.stream().collect(Collectors.groupingBy(SysRoleDataPermission::getOrgCode));
        Set<Map.Entry<String, List<SysRoleDataPermission>>> entries = orgMap.entrySet();

        List<OrgDataPermission> permissionList = new ArrayList<>();
        for (Map.Entry<String, List<SysRoleDataPermission>> orgEntry : entries) {
            Map<String, List<SysRoleDataPermission>> deptMap = orgEntry.getValue().stream().filter(e -> StringUtil.isNotBlank(e.getDeptCode())).collect(Collectors.groupingBy(SysRoleDataPermission::getDeptCode));
            List<DeptDataPermission> deptPermissionList = new ArrayList<>();
            for (Map.Entry<String, List<SysRoleDataPermission>> deptEntry : deptMap.entrySet()) {
                List<String> empCodes = deptEntry.getValue().stream().map(SysRoleDataPermission::getEmpCode).filter(StringUtil::isNotBlank).collect(Collectors.toList());
                DeptDataPermission deptDataPermission = new DeptDataPermission(deptEntry.getKey(), empCodes);
                deptPermissionList.add(deptDataPermission);
            }
            OrgDataPermission orgDataPermission = new OrgDataPermission(orgEntry.getKey(), deptPermissionList);
            permissionList.add(orgDataPermission);
        }
        System.out.println(JSON.toJSONString(permissionList));

        List<String> orgCodes = permissionList.stream().map(OrgDataPermission::getOrgCode).collect(Collectors.toList());
        System.out.println(orgCodes);

    }
}
